from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from common.Utils import oj_driver


# 测试搜索引擎的注册界面

class Register:
    url = ""
    driver = ""

    # 定义构造函数
    def __init__(self):
        self.url = "http://111.230.81.226:8080/register.html"
        self.driver = oj_driver.driver
        self.driver.get(self.url)

    # 注册成功的测试用例
    def RegisterSuccess(self):
        # 清空输入框中的内容
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # 输入测试用例
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("testonline")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("testonline")
        # 点击确定
        self.driver.find_element(By.CSS_SELECTOR, "#login-form > button:nth-child(6)").click()
        # 对弹窗的处理
        WebDriverWait(self.driver, 5).until(EC.alert_is_present())
        alert = self.driver.switch_to.alert
        alertval = alert.text
        assert (alertval == "注册成功")
        # 点击确认
        alert.accept()
        oj_driver.getScreenShot()

    # 多种注册失败的测试用例
    def RegisterFail(self):
        self.__usernamefail()
        self.__passwordfail()

    # 账号错误测试用例，包含三种可能
    def __usernamefail(self):
        self.__usernamelengthfail()
        self.__usernamecontentfail()
        self.__usernamelengthcontentfail()

    # 密码错误测试用例，包含一种可能
    def __passwordfail(self):
        self.__passwordlengthfail()

    # 账号长度错误
    def __usernamelengthfail(self):
        # 清空输入框中的内容
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # 输入测试用例
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("12")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        # 点击确定
        self.driver.find_element(By.CSS_SELECTOR, "#login-form > button:nth-child(6)").click()
        # 错误信息的检测
        self.driver.find_element(By.CSS_SELECTOR, "#error-message > p")
        oj_driver.getScreenShot()

    # 账号内容错误
    def __usernamecontentfail(self):
        # 清空输入框中的内容
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # 输入测试用例
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("12*345")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        # 点击确定
        self.driver.find_element(By.CSS_SELECTOR, "#login-form > button:nth-child(6)").click()
        # 错误信息的检测
        self.driver.find_element(By.CSS_SELECTOR, "#error-message > p")
        oj_driver.getScreenShot()

    # 账号长度内容都错误
    def __usernamelengthcontentfail(self):
        # 清空输入框中的内容
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # 输入测试用例
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("1*")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        # 点击确定
        self.driver.find_element(By.CSS_SELECTOR, "#login-form > button:nth-child(6)").click()
        # 错误信息的检测
        self.driver.find_element(By.CSS_SELECTOR, "#error-message > p")
        oj_driver.getScreenShot()

    # 密码长度错误
    def __passwordlengthfail(self):
        # 清空输入框中的内容
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # 输入测试用例
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("12345")
        # 点击确定
        self.driver.find_element(By.CSS_SELECTOR, "#login-form > button:nth-child(6)").click()
        # 错误信息的检测
        self.driver.find_element(By.CSS_SELECTOR, "#error-message > p")
        oj_driver.getScreenShot()
